package cn.com.gridinfo.sooc.core.expand.feedback.dao;

import com.fasterxml.jackson.databind.JsonNode;
import com.jeremy.arad.utils.JsonUtil;
import com.jeremy.arad.utils.Log;
import com.loopj.android.http.RequestParams;

import java.io.IOException;
import java.util.List;

import cn.com.gridinfo.sooc.base.http.SoocIDao;
import cn.com.gridinfo.sooc.base.http.SoocNetResult;
import cn.com.gridinfo.sooc.core.expand.feedback.CodeConst;

import cn.com.gridinfo.sooc.core.expand.feedback.bean.Suggestion;
import cn.com.gridinfo.sooc.utils.HttpUrl;

/**
 * 意见反馈 dao
 * Created by yingchunli on 2016/3/1.
 */
public class SuggestionDao extends SoocIDao {

    public String status;//状态 0 成功 1 服务器错误  2 逻辑错误
    public String statusCode;//提示信息
    public Suggestion suggestion;//反馈详情信息
    public List<Suggestion> suggestionList;//反馈列表

    public SuggestionDao(SoocNetResult iNetResult) {
        super(iNetResult);
    }

    @Override
    public void onRequestSuccess(JsonNode result, int requestCode) throws IOException {
        if (result == null) return;
        status = result.get("status").asText();
        statusCode = result.get("statusCode").asText();
        Log.d("result->" + result.toString());
        switch (requestCode) {

            //获取我的反馈
            case CodeConst.EXPAND_FEEDBACK_MYSUGGESTION_FIRST:
            case CodeConst.EXPAND_FEEDBACK_MYSUGGESTION_PUSHDOWN:
            case CodeConst.EXPAND_FEEDBACK_MYSUGGESTION_PULLUP:
                suggestionList = JsonUtil.node2pojoList(result.get("data"), Suggestion.class);
                break;

            //获取所有的反馈
            case CodeConst.EXPAND_FEEDBACK_ALLSUGGESTION_FIRST:
            case CodeConst.EXPAND_FEEDBACK_ALLSUGGESTION_PUSHDOWN:
            case CodeConst.EXPAND_FEEDBACK_ALLSUGGESTION_PULLUP:
                suggestionList = JsonUtil.node2pojoList(result.get("data"), Suggestion.class);
                break;

            //获取反馈详情列表
            case CodeConst.EXPAND_FEEDBACK_SUGGESTIONINFO_FIRST:

            case CodeConst.EXPAND_FEEDBACK_SUGGESTIONINFO_PULLUP:
            case CodeConst.EXPAND_FEEDBACK_SUGGESTIONINFO_PUSHDOWN:
                suggestion = JsonUtil.node2pojo(result.get("data"), Suggestion.class);
                break;
        }

    }

    /**
     * 提交反馈
     *
     * @param suggestion 反馈内容（长度无限制）
     */
    public void summitSuggestion(String suggestion) {
        String url = HttpUrl.getHttpUrl("Feedback", "addfeedback", true);
        Log.d("：url->" + url);
        RequestParams params = new RequestParams();
        params.add("ztnr", suggestion);
        postRequest(url, params, CodeConst.EXPAND_FEEDBACK_SUMMIT);
    }

    /**
     * 获取反馈列表
     *
     * @param my          0全部反馈，1我发表的反馈，默认0
     * @param key         搜索的关键字
     * @param page        显示的页数，默认1
     * @param num         每页显示的条数，默认10
     * @param requestCode 请求码
     */
    public void getSuggestionList(String my, String key, int page, int num, int requestCode) {
        String url = HttpUrl.getHttpUrl("Feedback", "feedbacklist", true);
        Log.d("：url->" + url);
        RequestParams params = new RequestParams();
        params.add("my", my);
        if (key != null)
            params.add("keyword", key);
        params.add("page", page + "");
        params.add("num", num + "");
        postRequest(url, params, requestCode);
    }

    /**
     * 获取反馈详情
     *
     * @param ztid        反馈id
     * @param page        回复显示的页数，默认1
     * @param num         回复每页显示的条数，默认10
     * @param requestCode 请求码
     */
    public void getSuggestionInfo(String ztid, int page, int num, int requestCode) {
        String url = HttpUrl.getHttpUrl("Feedback", "feedbackdetail", true);
        Log.d("：url->" + url);
        RequestParams params = new RequestParams();
        params.add("ztid", ztid);
        params.add("page", page + "");
        params.add("num", num + "");
        postRequest(url, params, requestCode);
    }

    /**
     * 回复反馈
     *
     * @param ztid 反馈id
     * @param hfnr 回复内容
     */
    public void hfSuggestion(String ztid, String hfnr) {
        String url = HttpUrl.getHttpUrl("Feedback", "replyfeedback", true);
        Log.d("：url->" + url);
        RequestParams params = new RequestParams();
        params.add("ztid", ztid);
        params.add("hfnr", hfnr);
        postRequest(url, params, CodeConst.EXPAND_FEEDBACK_HF);

    }

    public String getStatus() {
        return status;
    }

    public String getStatusCode() {
        return statusCode;
    }

    public List<Suggestion> getSuggestionList() {
        return suggestionList;
    }

    public Suggestion getSuggestion() {
        return suggestion;
    }
}
